//
//  LWSecCodeCtrl.m
//  laowu
//
//  Created by 曹秀云 on 15/5/1.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWSecCodeCtrl.h"
#import "TimeUtils.h"
#import "LWResetSuccCtrl.h"


@interface LWSecCodeCtrl ()
{
    NSTimer *_countDownTimer;
    int time;
}

@end

@implementation LWSecCodeCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"填写验证码"];
    [self.SecCodeTextFiled setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    time  = 60;
    [self startCountDown];
    [self.RetransmissionBtn addTarget:self action:@selector(getCodeClick:) forControlEvents:UIControlEventTouchUpInside];
     [self.NextStepBtn addTarget:self action:@selector(nextStepClick:) forControlEvents:UIControlEventTouchUpInside];
    
}


/** 
 开始倒计时的操作
 */
- (void)startCountDown {
    //处理倒计时
    NSString* time_str = [NSString stringWithFormat:@"倒计时%d秒",time];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
    [self.RetransmissionBtn  setEnabled:NO];
    [self.RetransmissionBtn  setTitle:time_str forState:UIControlStateDisabled];
}


/**
 *  倒计时调用的方法
 */
-(void)timeFireMethod{
    if(time <= 0){
        [_countDownTimer invalidate];
        [self.RetransmissionBtn setEnabled:YES];
        [self.RetransmissionBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        time = 60;
    }else{
        time--;
        NSString *timeStr = [NSString stringWithFormat:@"倒计时%d秒",time];
        [self.RetransmissionBtn setTitle:timeStr forState:UIControlStateDisabled];
    }
}

/**
 再次获取短信验证码的操作
 */

-(void)getCodeClick:(UIButton *)btn{
    [MeDataTool getVeriCodeWithPhone:self.PhoneNumLabel.text success:^(BaseResponse *result) {
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"获取验证码成功！"];
            [self.navigationController pushViewController:[[LWSecCodeCtrl alloc] init] animated:YES];
            [self startCountDown];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"获取验证码失败！"];
    }];
    
}

/** 
 验证短信码是否正确的操作
 */

-(void)nextStepClick:(UIButton *)btn{
    if(ISBLANK(self.SecCodeTextFiled.text)){
       [DialogUtils showTipsWithView:nil String:@"验证码为空！"];
        return;
    }
   }


@end
